local skel = fk.CreateSkill {
  name = "emo__weiming",
  dynamic_desc = function (self, player, lang)
    local list = {}
    for i = 1, 4 do
      if i <= player:getMark("emo__weiming-phase") then
        table.insert(list, "")
      else
        table.insert(list, "随机")
      end
    end
    return "emo__weiming_dyn:" .. table.concat(list, ":")
  end,
}

Fk:loadTranslationTable{
  ["emo__weiming"] = "危鸣",
  [":emo__weiming"] = "出牌阶段限四次，你可以令随机两名角色各摸一张牌，你与其中随机一名角色拼点，赢的角色获得另一个摸牌者随机两张牌。若你赢，你可以将一张获得的牌当【杀】使用；你没赢，你随机重铸一张牌，并于本阶段删除首个“随机”。",

  [":emo__weiming_dyn"] = "出牌阶段限四次，你可以令{1}两名角色各摸一张牌，你与其中{2}一名角色拼点，赢的角色获得另一个摸牌者{3}两张牌。若你赢，你可以将一张获得的牌当【杀】使用；你没赢，你{4}重铸一张牌，并于本阶段删除首个“随机”。",

  ["#emo__weiming"] = "令随机两名角色各摸一张牌，与其中一名拼点",
  ["#emo__weiming-choose"] = "选择一名角色，与其拼点",
  ["#emo__weiming-slash"] = "你可以将一张获得的牌当【杀】使用",
  ["#emo__weiming-recast"] = "危鸣：重铸1张牌",
}

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#emo__weiming",
  can_use = function (self, player)
    return #Fk:currentRoom().alive_players > 1 and player:usedSkillTimes(skel.name, Player.HistoryPhase) < 4
  end,
  card_filter = Util.FalseFunc,
  card_num = 0,
  times = function (self, player)
    return 4 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  target_filter = function (self, player, to, selected)
    return player:getMark("emo__weiming-phase") > 0 and #selected < 2
  end,
  target_num = 1,
  feasible = function (self, player, selected)
    if player:getMark("emo__weiming-phase") == 0 then
      return #selected == 0
    else
      return #selected == 2
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local mark = player:getMark("emo__weiming-phase")
    local tos = table.simpleClone(effect.tos)
    if #tos == 0 then
      tos = table.random(room.alive_players, 2)
    end
    room:sortByAction(tos)
    room:doIndicate(player, tos)
    room:delay(200)
    for _, to in ipairs(tos) do
      if not to.dead then
        to:drawCards(1, skel.name)
      end
    end
    local targets = table.filter(tos, function (to)
      return not to.dead and player:canPindian(to)
    end)
    if #targets > 0 then
      local to = table.random(targets)
      if mark > 1 then
        to = room:askToChoosePlayers(player, {
          targets = targets, min_num = 1, max_num = 1, skill_name = skel.name, prompt = "#emo__weiming-choose", cancelable = false,
        })[1]
      end
      local pindian = player:pindian({to}, skel.name)
      local winner = pindian.results[to].winner
      local cards = {}
      if winner and not winner.dead then
        table.removeOne(tos, to)
        local other = tos[1]
        if other and not other:isNude() and winner ~= other then
          room:doIndicate(winner, other)
          local num = math.min(2, #other:getCardIds("he"))
          cards = mark < 3 and table.random(other:getCardIds("he"), num) or
          room:askToChooseCards(winner, {target = other, flag = "he", min = num, max = num, skill_name = skel.name })
          room:obtainCard(winner, cards, false, fk.ReasonPrey, winner, skel.name)
        end
      end
      if winner == player then
        cards = table.filter(cards, function (id)
          return table.contains(player.player_cards[Player.Hand], id)
        end)
        if player:isAlive() and #cards > 0 then
          room:askToUseVirtualCard(player, {
            name = "slash", skill_name = skel.name, prompt = "#emo__weiming-slash", skip = false,
            card_filter = {n = {1,1}, cards = cards }
          })
        end
        return
      end
    end
    if not player:isNude() then
      local cid
      if mark < 4 then
        cid = table.random(player:getCardIds("he"))
      else
        cid = room:askToCards(player, {min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, cancelable = false})[1]
      end
      room:recastCard({cid}, player, skel.name)
    end
    if player.dead then return end
    room:addPlayerMark(player, "emo__weiming-phase", 1)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "emo__weiming-phase", 0)
end)

return skel
